package com.jiangnan.controller;

import com.jiangnan.anno.Log;
import com.jiangnan.pojo.Clazz;
import com.jiangnan.pojo.ClazzQueryParam;
import com.jiangnan.pojo.PageResult;
import com.jiangnan.pojo.Result;
import com.jiangnan.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    /**
     * 分页查询
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam) {
        log.info("分页查询，参数：{}", clazzQueryParam);
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 根据id删除班级
     * @param id
     * @return
     */
    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除班级，id：{}", id);
        clazzService.deleteById(id);
        return Result.success();
    }

    /**
     * 保存班级
     * @param clazz
     * @return
     */
    @Log
    @PostMapping
    public Result save(@RequestBody Clazz clazz) {
        log.info("保存班级，参数：{}", clazz);
        clazzService.save(clazz);
        return Result.success();
    }

    /**
     * 根据id查询班级
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询班级，id：{}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级
     * @param clazz
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级，参数：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        log.info("查询所有班级");
        List<Clazz> list = clazzService.list();
        return Result.success(list);
    }

}
